package com.example.demo.service.serviceImpl;

import com.example.demo.service.Tencent_SpeechService;
import com.example.demo.sign.TencentAISign;
import com.example.demo.sign.TencentAISignSort;
import com.example.demo.utils.*;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
@Service
@Scope("prototype")
public class Tencent_SpeechServiceImpl implements Tencent_SpeechService{
    @Override
    public String getVoiceToText(byte[] file, String format, String rate) throws Exception {
        //时间戳
        String time_stamp = System.currentTimeMillis()/1000+"";
        //随机字符串
        String nonce_str = TencentAISign.getRandomString(10);
        byte[] files = UrlMethodUtil.local2byte("C:/Users/Administrator/Desktop/VOICE1513237078.pcm");
        String img = Base64Util.encode(files);
        System.out.println(img);
        Map<String,String> body = new HashMap<>();   //【接口属性】（注意修改成自己要的参数）
        body.put("app_id", String.valueOf(TencentAPI.APP_ID_AI));
        body.put("time_stamp",time_stamp);
        body.put("nonce_str", nonce_str);
        body.put("format", format);
        body.put("speech", img);
        body.put("rate", rate);
        String sign = TencentAISignSort.getSignature(body);
        body.put("sign", sign);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        HttpResponse responseBD = HttpsUtil4Tencent.doPostTencentAI(TencentAPI.SPEECHRECOGNITION, headers, body);
        String json = EntityUtils.toString(responseBD.getEntity());
        return json;
    }
    @Override
    public String getAIToText(String question, String session) throws Exception {
        //时间戳
        String time_stamp = System.currentTimeMillis()/1000+"";
        //随机字符串
        String nonce_str = TencentAISign.getRandomString(10);
        Map<String,String> body = new HashMap<>();   //【接口属性】（注意修改成自己要的参数）
        body.put("app_id", String.valueOf(TencentAPI.APP_ID_AI));
        body.put("time_stamp",time_stamp);
        body.put("nonce_str", nonce_str);
        body.put("session", session);
        body.put("question", question);
        String sign = TencentAISignSort.getSignature(body);
        body.put("sign", sign);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        String json = SendPostUtil.sendPostUTF8(TencentAPI.NLP_ZHINENGXIANLIAO, body, headers);
        return json;
    }
}
